/**
 * @filename:SampleController 2022-03-09 07:00:40
 * @project com/lwl/blue  V1.0
 * Copyright(c) 2020 <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a> Co. Ltd.
 * All right reserved.
 */
package com.lwl.blue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lwl.blue.commont.base.*;
import com.lwl.blue.commont.utils.UserContextUtil;
import com.lwl.blue.entity.Sample;
import com.lwl.blue.entity.SampleDetail;
import com.lwl.blue.service.ItemService;
import com.lwl.blue.service.SampleDetailService;
import com.lwl.blue.service.SampleService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>自定义方法写在这里</p>
 *
 * <p>说明： 样品单资料API接口层</P>
 *
 * @version: V1.0
 * @author: <a href=mailto:lanwenliang@yidianlife.com>蓝文良</a>
 */
@Api(tags = "样品单资料")
@RestController
@RequestMapping("/sample")
@Slf4j
public class SampleController extends BaseController {
    @Resource
    SampleService sampleService;

    @PostMapping("/pages")
    public PageResult<Sample> getPages(@RequestBody PageParam<Sample> pageParam) {
        JsonResult<Page<Sample>> pages = sampleService.pages(pageParam);
        PageResult<Sample> re = new PageResult<>();
        re.success(pages.getData().getTotal(), pages.getData().getRecords());
        return re;
    }

    @PostMapping("saveOrUpdate")
    public JsonResult saveOrUpdate(@RequestBody Sample sample) {
        setInsetOrUpdateUser(sample);
        if (null == sample.getId()) {
            sample.setStatus("正常");
        }
        QueryWrapper<Sample> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(Sample::getSampleNo, sample.getSampleNo())
                .eq(Sample::getIsDelete, 0);
        if (null != sample.getId()) {
            wrapper.lambda().notIn(Sample::getId, sample.getId());
        }
        int count = sampleService.count(wrapper);
        if (count > 0) {
            throw new BizException(ErrorCode.DATA_EXISTS_ERROR.getCode(), ErrorCode.DATA_EXISTS_ERROR.getMsg() + sample.getSampleNo());
        }
        sampleService.saveUpdateData(sample);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/delete/{id}")
    public JsonResult delete(@PathVariable(value = "id") Integer id) {
        sampleService.deleteData(id);
        return new JsonResult().success("请求成功");
    }

    @PostMapping("/select")
    public JsonResult select() {
        List<Sample> reList = sampleService.list(new QueryWrapper<Sample>()
                .lambda().eq(Sample::getIsDelete, 0));
        return new JsonResult().success(reList);
    }

//    @GetMapping("/export/{params}")
//    public void export(HttpServletResponse response, @PathVariable("params") String params) throws IOException {
//        Sample sample = JSON.parseObject(params, Sample.class);
//        List<Sample> reList = sampleService.getExportList(sample);
//        List<SampleExport> itemExports = new ArrayList<>();
//        if (!reList.isEmpty()) {
//            reList.forEach(
//                    l -> {
//                        SampleExport SampleExport = new SampleExport();
//                        BeanUtils.copyProperties(l, SampleExport);
//                        itemExports.add(SampleExport);
//                    }
//            );
//        }
//        EasyExcelUtils.export(response, "报价单", ItemExport.class, itemExports);
//    }


}
